from odoo import models, api


class PurchaseOutsourceTemplateReport(models.AbstractModel):
    _name = "report.sb_subcontracting.purchase_outsource_template"
    _description = '采购外发模板'

    @api.model
    def _get_report_values(self, docids, data=None):
        outside_ids = self.env['stock.subcon.outside'].search([('id', 'in', docids)])
        data = []
        for outside in outside_ids:
            order_line = []
            for line in outside.line_ids:
                order_line.append({
                    'id': line.id,
                    'work': line.purchase_line_id.s_production_id.name,
                    'product_id': line.s_subcontracting_product_id.name,  # 型号
                    's_area': line.purchase_line_id.s_area,  # 平米
                    's_specs': line.s_specs,  # 规格 pnll
                    'product_uom': line.product_uom.name,  # 计量单位
                    'product_qty': line.product_qty,  # 数量
                    'remark': line.purchase_line_id.name,  # name
                })
            dtl = {
                'id': outside.id,
                'name': outside.name,
                'date_approve': outside.date_approve,
                'type': outside.line_ids[0].product_id.name if outside.line_ids else '',
                'partner_id': outside.partner_id.name,
                'order_line': order_line,
            }
            data.append(dtl)
        return {
            'report_title': 'Outsource Template(PDF)',
            'doc_model': 'stock.subcon.outside',
            'docs': docids,
            'data': data,
        }
